// pages/reimburse/reimburse.js
const util = require("../../utils/util.js");
var app = getApp();
const requestManager = require("../../utils/request.js");
var router = require('../../utils/router.js');
const pageName = "PersonalCenter "
const config = require("../../utils/config.js")
let orderRefundObj = {}
const baseUrl = config.baseUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerText: {
      des: "请选择",
      id: -1
    },
    refundData: [],
    detailData: null,
    productInfo: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderRefundObj.orderId = options.orderId
    orderRefundObj.serviceType = options.id?options.id:1
    this.getDetaildata(options.orderId)
    this.get_refund_desc()
  },
  bindTextAreaBlur(e){
    orderRefundObj.note = e.detail.value
  },
  // 选择退款原因
  selectPicker(event){
    this.setData({
      pickerText: this.data.refundData[event.detail.value]
    })
    orderRefundObj.refundDesc = this.data.refundData[event.detail.value].id
  },
  //获取退款原因
  get_refund_desc() {
    requestManager.getDataResult(requestManager.refund_desc, {}, "GET").then(res => {
      this.setData({
        refundData: res.list ? res.list : []
      })
    })
  },
  //获取订单详情数据
  getDetaildata(productid) {
    let params = {
      userId: util.getUserInfo().userId,
      id: productid || 82659
    }
    requestManager.getDataResult(requestManager.order_view, params, "GET").then(res => {
      this.setData({
        detailData: res.data
      })
    })
  },
  // 删除图片
  handleDelete(event) {
    let currentUrl = event.currentTarget.dataset.url;
    let oldImg = this.data.productInfo;
    let currentId = oldImg.findIndex(item => item.previewPath === currentUrl);
    oldImg[currentId].flag = false
    this.setData({
      productInfo: oldImg
    });
  },
  // 提交
  handleRefund(event) {
    if(this.data.pickerText.id==-1){
     return wx.showToast({
       title:'请选择退款原因',
       icon:'none'
     })
    }
    orderRefundObj.userId=util.getUserInfo().userId
    requestManager.getDataResult(requestManager.order_refund, orderRefundObj, "POST").then(res => {
      let url = "/pages/initiaterefund/initiaterefund";
      router.route.setAction(router.NAVIGATE_TO)
        .setParams([`orderId=${orderRefundObj.orderId}`])
        .setUrl(url)
        .run()
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //上传图片
  bindChooiceProduct: function () {
    var that = this;
    wx.chooseImage({
      count: 6, //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(2222, tempFilePaths)
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: baseUrl + requestManager.mulit_upload,
            filePath: tempFilePaths[i],
            name: 'imageFile',
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              uploadImgCount++;
              var data = JSON.parse(res.data);
              let currentImgurl = data.result.imageRoute[0]
              currentImgurl.flag = true
              var productInfo = that.data.productInfo;
              productInfo.push(currentImgurl);
              that.setData({
                productInfo: productInfo
              });
              let arrImages = []
              productInfo.forEach((v=>{
                arrImages.push(v.previewPath)
              }))
              orderRefundObj.imagesFiles = arrImages
              //如果是最后一张,则隐藏等待中
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
      }
    });
  }
})